#include "ItemReferenceProxy.h"



ItemReferenceProxy::ItemReferenceProxy(Item* targetItem, QAbstractItemModel* sourceModel, QObject *parent) :
    QAbstractProxyModel(parent)
{
    m_targetItem = targetItem;
    setSourceModel(sourceModel);

    remap();
    connect(sourceModel, SIGNAL(layoutChanged()), this, SLOT(remap()));
}



QModelIndex ItemReferenceProxy::mapFromSource(const QModelIndex &sourceIndex) const
{
    if (!sourceIndex.isValid()) return QModelIndex();

    return m_mapping.value(sourceIndex);
}



QModelIndex ItemReferenceProxy::mapToSource(const QModelIndex &proxyIndex) const
{
    if (!proxyIndex.isValid()) return QModelIndex();

    return m_mapping.key(proxyIndex);
}



QVariant ItemReferenceProxy::data(const QModelIndex &proxyIndex, int role) const
{
    if (!proxyIndex.isValid()) return QVariant("Invalid proxy index");

    QModelIndex sourceIndex = mapToSource(proxyIndex);
    return sourceIndex.data(role);
}



QModelIndex ItemReferenceProxy::parent(const QModelIndex &child) const
{
    Q_UNUSED(child);
    return QModelIndex();
}



QModelIndex ItemReferenceProxy::index(int row, int column, const QModelIndex &parent) const
{
    if (!hasIndex(row, column, parent) || parent.isValid())
    {
        //qDebug("Searching proxy index: parent has no index!");
        return QModelIndex();
    }

    foreach (QPersistentModelIndex pindex, m_mapping.values())
    {
        if (pindex.row() != row) continue;
        if (pindex.column() != column) continue;

        return pindex;
    }

    return QModelIndex();
}



int ItemReferenceProxy::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid()) return 0;

    return (DataType_LAST - DataType_FIRST + 1);
}



int ItemReferenceProxy::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid()) return 0;

    return m_mapping.count();
}



void ItemReferenceProxy::remap()
{
    m_mapping.clear();

    QAbstractItemModel* source = sourceModel();
    if (!source) return;

    int proxy_row = 0;
    for (int group_row = 0; group_row < source->rowCount(); group_row++)
    {
        QModelIndex parentIndex = source->index(group_row, 0);
        if (!parentIndex.isValid())
        {
            qDebug(QString("Group model source index not valid at row %1").arg(group_row).toAscii());
            continue;
        }

        for (int item_row = 0; item_row < source->rowCount(parentIndex); item_row++)
        {
            QModelIndex nameIndex = source->index(item_row, AbstractItem::NameData, parentIndex);
            QModelIndex idIndex = source->index(item_row, AbstractItem::IdData, parentIndex);

            if (!nameIndex.isValid() || !idIndex.isValid())
            {
                qDebug(QString("Item model name or id index not valid at row %1").arg(group_row).toAscii());
                continue;
            }

            m_mapping.insert(QPersistentModelIndex(nameIndex), QPersistentModelIndex(createIndex(proxy_row, NameData, nameIndex.internalPointer())));
            m_mapping.insert(QPersistentModelIndex(idIndex), QPersistentModelIndex(createIndex(proxy_row, IdData, idIndex.internalPointer())));
            proxy_row++;
        }
    }
}


#if 0
bool ItemReferenceProxy::submit()
{
    return true;
}
#endif





